package com.example.service.impl;

import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> getAllUsers() {
        return userDao.findAllUsers();
    }

    @Override
    public User getUserById(Integer id) {
        return userDao.findUserById(id);
    }

    @Override
    public User getUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }

    @Override
    @Transactional
    public boolean addUser(User user) {
        if (user == null) {
            return false;
        }

        // 设置创建时间
        user.setCreateTime(new Date());

        return userDao.addUser(user) > 0;
    }

    @Override
    @Transactional
    public boolean updateUser(User user) {
        if (user == null || user.getId() == null) {
            return false;
        }

        return userDao.updateUser(user) > 0;
    }

    @Override
    @Transactional
    public boolean deleteUser(Integer id) {
        if (id == null) {
            return false;
        }

        return userDao.deleteUser(id) > 0;
    }
}
